package com.rtx.job.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.job.bean.dto.EdiSqlDto;
import com.rtx.job.bean.po.EdiSql;
import com.rtx.job.service.EdiSqlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = {"EDI任务SQL配置"})
@RestController
@RequestMapping("/v1/ediSql")
@CrossOrigin
public class EdiSqlController {

    @Resource
    private EdiSqlService ediSqlService;

    /**
     * 获取EDI任务SQL配置列表
     * @return
     */
    @ApiOperation(value = "获取EDI任务SQL配置列表", notes = "分页获取EDI任务SQL配置列表")
    @PostMapping("selectEdiSqlList")
    public EdiResponse selectEdiSqlList(@RequestBody EdiSqlDto EdiSqlDto){
        EdiSql EdiSql = new EdiSql();
        BeanUtils.copyProperties(EdiSqlDto,EdiSql);
        Page<EdiSql> pageParam = new Page<>(EdiSqlDto.getPageNum(), EdiSqlDto.getPageSize());
        ediSqlService.selectEdiSqlList(pageParam,EdiSql);
        return EdiResponse.success().data("page",pageParam);
    }

    /**
     * 根据ID获取EDI任务SQL配置
     * @param EdiSqlDto
     * @return
     */
    @ApiOperation(value = "根据ID获取EDI任务SQL配置", notes = "根据ID获取EDI任务SQL配置")
    @PostMapping("selectEdiSqlById")
    public EdiResponse selectEdiSqlById(@RequestBody EdiSqlDto EdiSqlDto){
        return EdiResponse.success().data("item",ediSqlService.getById(EdiSqlDto.getId()));
    }

    /**
     * 根据ID删除EDI任务SQL配置
     * @param EdiSqlDto
     * @return
     */
    @ApiOperation(value = "根据ID删除EDI任务SQL配置", notes = "根据ID删除EDI任务SQL配置")
    @PostMapping("deleteEdiSqlById")
    public EdiResponse deleteEdiSqlById(@RequestBody EdiSqlDto EdiSqlDto){
        return ediSqlService.removeById(EdiSqlDto.getId()) ? EdiResponse.success() : EdiResponse.failed();
    }

    /**
     * 根据ID批量删除EDI任务SQL配置
     * @param list
     * @return
     */
    @ApiOperation(value = "根据ID批量删除EDI任务SQL配置", notes = "根据ID批量删除EDI任务SQL配置")
    @PostMapping("deleteEdiSqlByIds")
    public EdiResponse deleteEdiSqlByIds(@RequestBody List<EdiSqlDto> list){
        return ediSqlService.removeBatchByIds(list) ? EdiResponse.success() : EdiResponse.failed();
    }

    /**
     * 新增EDI任务SQL配置
     * @param EdiSqlDto
     * @return
     */
    @ApiOperation(value = "新增EDI任务SQL配置", notes = "新增EDI任务SQL配置")
    @PostMapping("addEdiSql")
    public EdiResponse addEdiSql(@RequestBody EdiSqlDto EdiSqlDto){
        EdiSql EdiSql = new EdiSql();
        BeanUtils.copyProperties(EdiSqlDto,EdiSql,"id");
        return ediSqlService.save(EdiSql) ? EdiResponse.success() : EdiResponse.failed();
    }

    /**
     * 修改EDI任务SQL配置
     * @param EdiSqlDto
     * @return
     */
    @ApiOperation(value = "修改EDI任务SQL配置", notes = "修改EDI任务SQL配置")
    @PostMapping("editEdiSql")
    public EdiResponse editEdiSql(@RequestBody EdiSqlDto EdiSqlDto){
        EdiSql EdiSql = new EdiSql();
        BeanUtils.copyProperties(EdiSqlDto,EdiSql);
        return ediSqlService.updateById(EdiSql) ? EdiResponse.success() : EdiResponse.failed();
    }
}
